#include <stdio.h>

long long returnReflectLength(long long wide, long long sideLength) {
    // 将反射过程看成是在多个隧道内连续反射
    if (sideLength % wide == 0) { return (sideLength / wide) * wide * 2 - wide; }
    else {
        return (sideLength / wide) * wide * 2 + returnReflectLength(sideLength % wide, wide);
    }
}

int main() {
    long long sideLength, startPoint;
    scanf("%lld%lld", &sideLength, &startPoint);
    long long result = sideLength;
    result += returnReflectLength(startPoint, sideLength - startPoint);
    printf("%lld", result);
}